﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using System.Data;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class BillReturnVisitList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList(1);
            }
        }
        private void BindList(int pageindex)
        {
            string Whereclause = "";
            if (txtPaperInvoicesNo.Text.Trim() != "")
            {
                Whereclause = " and MaintainID in (select MaintainID from Cms_Wsm_MaintainBill where PaperInvoicesNo like '%" + txtPaperInvoicesNo.Text.Trim() + "%')";
            }
            if (txtCustomerName.Text.Trim() != "")
            {
                Whereclause = " and MaintainID in (select MaintainID from Cms_Wsm_MaintainBill where registerid in (select registerid from Cms_Wsm_TaskRegister where customerid in(select customerid from cms_ctrt_customer where customername  like '%" + txtCustomerName.Text.Trim() + "%')))";
            }
            if (txtInstrumentName.Text.Trim() != "")
            {
                Whereclause = " and MaintainID in (select MaintainID from Cms_Wsm_MaintainBill where registerid in (select registerid from Cms_Wsm_TaskRegister where InstrumentID in(select instrumentid from Cms_Wsm_InstrumentInfo where instrumentName  like '%" + txtInstrumentName.Text.Trim() + "%')))";
            }
            if (txtMaintainer.Text.Trim() != "")
            {
                Whereclause = " and MaintainID in (select MaintainID from Cms_Wsm_MaintainBill where maintainer in(select userid from cms_sys_user where userName like '%" + txtMaintainer.Text.Trim() + "%'))";
            }
            int RecordCount = 0;
            List<WsmMaintainEvaluationEntity> list = new SystemBll().GetAll<WsmMaintainEvaluationEntity>(Whereclause, "CreatedOn desc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterEvaluation.DataSource = list;
            RepeaterEvaluation.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void RepeaterEvaluation_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindList(1);
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList(1);
        }

        protected void RepeaterEvaluation_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label maintainPaperNum = e.Item.FindControl("maintainPaperNum") as Label;
            Label EvaluationID = e.Item.FindControl("EvaluationID") as Label;
            Label MaintainerName = e.Item.FindControl("MaintainerName") as Label;
            Label CustomerName = e.Item.FindControl("CustomerName") as Label;
            Label InstrumentName = e.Item.FindControl("InstrumentName") as Label;
            Label CreatedBy = e.Item.FindControl("CreatedBy") as Label;
            Label CreatedByName = e.Item.FindControl("CreatedByName") as Label;
            Label IsTrack = e.Item.FindControl("IsTrack") as Label;

            if (EvaluationID != null)
            {
                WsmMaintainEvaluationEntity entity = new SystemBll().Get<WsmMaintainEvaluationEntity>(new Guid(EvaluationID.Text));
                WsmMaintainBillEntity maintainEntity = new SystemBll().Get<WsmMaintainBillEntity>(entity.MaintainID);
                WsmTaskRegisterEntity registerEntity = new SystemBll().Get<WsmTaskRegisterEntity>(maintainEntity.RegisterID);
                maintainPaperNum.Text = maintainEntity.PaperInvoicesNo;
                if (maintainEntity.Maintainer != Guid.Empty)
                {
                    MaintainerName.Text = new SystemBll().Get<SysUserEntity>(maintainEntity.Maintainer).UserName;
                }
                CustomerName.Text = new SystemBll().Get<CtrtCustomerEntity>(registerEntity.CustomerID).CustomerName;
                InstrumentName.Text = new SystemBll().Get<WsmInstrumentInfoEntity>(registerEntity.InstrumentID).instrumentName;
                CreatedByName.Text = new SystemBll().Get<SysUserEntity>(new Guid(CreatedBy.Text)).UserName;
                IsTrack.Text = entity.IsTrack == 1 ? "√" : "";
            }
        } 
    }
}
